#Powershell ConvertTo-JSON MongoDB Coding
Explore tagged Tumblr posts
Text
ConvertTo/From-JSON MongoDB
Learning the quirks of powershell has had some benefits and some things that have made me irritated. I got an error that my type could not be put into another type. I’ve got a 3rd party vender that we use for our CRM and we use MongoDB to do some more dynamic signage types of things in our office. In doing this simple pull from one and putting it into another, everything seems fine, except the C# won’t take one field (it has an array, I know why it won’t, but irritating) so I went through some options to try and put the square through the round hole. I tried to flatten, but seems that flattening it wasn’t working, tried to convert it a few times a few different ways in hopes that the converted changes would possibly get it to work. Nothing seemed to work, I spent quite a bit of time and I was ready to attempt some very extreme measures when I thought. What if I converted it to JSON and back through powershell. This is very, very silly to think of working, but sure enough, it worked great.. Well, for my one entry I was pulling at least.
Here is kind of what I had made and got to work, if you have something that works without the JSON conversion, sweet.
Get-Data -Entity DataTable -Filter {idcolumn -eq 5} | ConvertTo-JSON | ConvertFrom-JSON | Add-Mdbc-Data
Now the aha moment was running this and it actually put my array inside of the database and only had one document, what caught me was changing it to pull in more data like this:
Get-Data -Entity DataTable -Filter {idcolumn -gt 5} | ConvertTo-JSON | ConvertFrom-JSON | Add-Mdbc-Data
What I was greeted with when I ran this almost identical statement with a filter that should have grabbed several more? 1 Document. It made one freaking document and from the look of it, the array didn’t come in (because the item I looked at didn’t have one) but after a few more attempts to change my filter, I found out what was going on. I had 1 document, containing my hundreds of results for my query. So it put a level above it as if it made a database inside of the database. That could have been worked with, but boy what a pain. So what happened? Why am I getting this extra layer when it should have just grabbed the data and then put it back. Now I have to thank this post for the fix - https://stackoverflow.com/questions/20848507/why-does-powershell-give-different-result-in-one-liner-than-two-liner-when-conve After a couple hours trying to figure out conversion, I finally resolved it, this is what it ended looking like:
((Get-Data -Entity DataTable -Filter {idcolumn -gt 5} | ConvertTo-JSON) | ConvertFrom-JSON ) | Add-Mdbc-Data
So after a bit of troubleshooting, I found out that doing it that way limits what can go over the pipeline, making it only have one option instead of a chance of multiple.
0 notes